/*
 * Log.cpp
 *
 *  Created on: Sep 11, 2011
 *      Author: kimi
 */

#include "Log.h"

void Log::info (string label, string detail) {
	log("INFO",label,detail);
}

void Log::error (string label, string detail) {
	log("ERROR",label,detail);
}

void Log::warning (string label, string detail) {
	log("WARN",label,detail);
}

void Log::plain (string detail) {
	logFileStream << detail << endl;
	logFileStream.flush();
}

void Log::log(string type, string label, string detail) {
	time_t now = time(NULL);
	struct tm * ptm = localtime(&now);
	char timestamp[32];
	strftime (timestamp, 32, "%d.%m.%Y %H:%M:%S", ptm);

	logFileStream << timestamp << "\t[" << type << "]\t" << label << "\t\t" << detail << endl;
	logFileStream.flush();
}

void Log::close() {
	logFileStream.flush();
	logFileStream.close();
}

void Log::newLine () {
	logFileStream << endl;
	logFileStream.flush();
}

Log::Log(string logFilePath) {
	// abro archivo de log
//	cout << "Creando log en: "<<logFilePath << endl;
	filePath = logFilePath;
	logFileStream.open(filePath.c_str(), fstream::in | fstream::out | fstream::app);


	if (!logFileStream.is_open()) {
		logFileStream.open(filePath.c_str(), fstream::in | fstream::out | fstream::trunc);
	}

	//TODO: Mejorar usando el logger.
	if (!logFileStream.is_open()) {
		throw ios_base::failure("cannot create file at \"" + logFilePath + "\".");
	}
}

Log::~Log() {
	close();
}


